﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jurassic.Library;
using Flower.Services;
using Jurassic;
using Flower.Directory.Scripting;
using Flower.Directory.Util;
using Flower.Services.Data;
using Flower.Directory.Host.Default;

namespace Flower.Directory.Management.Scripting
{
    public class ProcessorObject : ObjectInstance
    {
        private readonly IProcessor _processor;

        public ProcessorObject(ObjectInstance prototype, IProcessor processor)
            : base(prototype)
        {
            _processor = processor;

            PopulateFunctions();
        }

        [JSFunction(Name = "start")]
        public void Start()
        {
            try
            {
                _processor.IsRunning(true);
            }
            catch (Exception ex)
            {
                throw new JavaScriptException(Engine, "Error", ex.Message);
            }
        }

        [JSFunction(Name = "stop")]
        public void Stop()
        {
            try
            {
                _processor.IsRunning(false);
            }
            catch (Exception ex)
            {
                throw new JavaScriptException(Engine, "Error", ex.Message);
            }
        }
    }

    public class ProcessorObjectConstructor : ClrFunction
    {
        public ProcessorObjectConstructor(ScriptEngine engine)
            : base(
                engine.Function.InstancePrototype,
                "processor",
                engine.Object.InstancePrototype
            )
        {
        }

        [JSCallFunction]
        public ProcessorObject Create(string processorName)
        {
            var dir = Engine.GetGlobalValue("$dir") as DirectoryObject;

            if (dir == null)
            {
                throw new JavaScriptException(Engine, "Error", "The directory is not initialized.");
            }

            try
            {
                var proc = dir.Directory.Get("/Processors/" + processorName) as Processor;

                if (proc == null)
                {
                    throw new JavaScriptException(Engine, "Error", "The processor not found.");
                }

                if (proc.Configuration == null)
                {
                    throw new JavaScriptException(Engine, "Error", "The processor has no endpoint configured property.");
                }

                var client = DirectoryUtil.GetService<IProcessor>(proc, "processor");

                if (client == null)
                {
                    throw new JavaScriptException(Engine, "Error", "The processor has no endpoint configured property.");
                }

                return new ProcessorObject(Engine.Object.InstancePrototype, client);
            }
            catch (Exception ex)
            {
                throw new JavaScriptException(Engine, "Error", string.Format("Failed to create an instance of a processor: {0}.", ex.Message), ex);
            }
        }
    }

}
